<template>
  <div>
    <PageTools>
      <!-- 左侧具名插槽 -->
      <template #left>
        <el-tag>
          <i class="el-icon-info" />
          <span class="span">共{{ total }}条记录</span>
        </el-tag>
      </template>
      <!-- 右侧具名插槽 -->
      <template #right>
        <div>
          <div class="float">
            <el-button type="danger" size="mini">普通excel导出</el-button>
            <el-button type="info" size="mini">复杂表头excel导出</el-button>
            <el-button type="success" size="mini" @click="$router.push('/import')">excel导入</el-button>
            <el-button type="primary" size="mini" @click="showDialog">新增员工</el-button>
          </div>
        </div>
      </template>
    </PageTools>

    <el-card>
      <!-- 表格区域 -->
      <el-table :data="list" border style="width: 100%">
        <el-table-column type="index" label="序号" width="100px" align="center" />
        <el-table-column prop="username" label="姓名" />
        <el-table-column prop="staffPhoto" label="头像" align="center" />
        <el-table-column prop="mobile" label="手机号" />
        <el-table-column prop="workNumber" label="工号" />
        <el-table-column label="聘用形式">
          <template #default="scope">
            {{ hireMap[+scope.row.formOfEmployment] || '未知' }}
          </template>
        </el-table-column>
        <el-table-column prop="departmentName" label="部门" />
        <el-table-column prop="timeOfEntry" label="入职时间">
          <template #default="scope">
            {{ formateTime(scope.row.timeOfEntry) }}
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <el-switch
            disabled
          />
        </el-table-column>
        <el-table-column label="操作" width="280px">
          <template #default="scope">
            <el-button type="text" size="mini">查看</el-button>
            <el-button type="text" size="mini">转正</el-button>
            <el-button type="text" size="mini">调岗</el-button>
            <el-button type="text" size="mini">离职</el-button>
            <el-button type="text" size="mini" @click="rolesFn(scope.row.id)">角色</el-button>
            <el-button type="text" size="mini" @click="delEmployee(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-row type="flex" justify="end" align="middle">
        <el-pagination
          layout="total,prev, pager, next"
          :total="total"
          :page-size="params.size"
          :current-page="params.page"
          hide-on-single-page
          @current-change="handleCurrentChange"
        />
      </el-row>
    </el-card>
    <!-- 添加弹窗  子组件 -->
    <AddDialog :visible="dialogVisible" @close="closeDialog" @fetch-employee-list="fetchEmployeeList" />
    <!-- 角色弹窗  子组件 -->
    <RolesDialog :id="currentEmployeeId" :visible="rolesDialogVisible" @close="closeFn" />
  </div>
</template>

<script>
import { getEmployeeList, delEmployee } from '@/api'
import dayjs from 'dayjs'
import AddDialog from './components/AddDialog.vue'
import RolesDialog from './components/RolesDialog.vue'
export default {
  components: {
    AddDialog, RolesDialog
  },
  data() {
    return {
      params: {
        // 页码
        page: 1,
        // 每页条数
        size: 10
      },
      // 员工列表
      list: [],
      // 总条数
      total: 0,
      // 定义聘用形式的枚举
      hireMap: {
        1: '正式',
        2: '非正式'
      },
      // 控制弹窗显示与隐藏
      dialogVisible: false,
      // 控制子组件弹框显示与隐藏
      rolesDialogVisible: false,
      // 当前分配角色的员工id
      currentEmployeeId: ''
    }
  },
  created() {
    this.fetchEmployeeList()
  },
  methods: {
    // 获取员工列表
    async fetchEmployeeList() {
      const { data } = await getEmployeeList(this.params)
      // console.log(data)
      this.list = data.rows
      this.total = data.total
    },
    // 时间格式化的方法，利用dayjs
    formateTime(value) {
      return dayjs(value).format('YYYY-MM-DD')
    },
    // 当前页current-page改变时会触发
    handleCurrentChange(newPage) {
      this.params.page = newPage
      this.fetchEmployeeList()
    },
    // 展示新增弹框
    showDialog() {
      this.dialogVisible = true
    },
    // 关闭新增弹窗，子传父自定义事件
    closeDialog() {
      this.dialogVisible = false
    },
    // 删除员工
    delEmployee(id) {
      this.$confirm('此操作将永久删除该员工, 是否继续?', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delEmployee(id)
        this.$message.success('删除成功！')
        // 带有分页的页面删除时需要考虑边界情况
        if (this.params.page > 1 && this.list.length === 1) {
          this.params.page--
        }
        this.fetchEmployeeList()
      }).catch(error => {
        console.log(error)
      })
    },
    // 点击 角色触发
    rolesFn(id) {
      this.rolesDialogVisible = true
      this.currentEmployeeId = id
    },
    // 角色弹窗关闭，子传父自定义事件
    closeFn() {
      this.rolesDialogVisible = false
      this.fetchEmployeeList()
    }
  }
}
</script>

<style lang="scss" scoped>
.float{
  float: right
}
.el-card{
  margin: 15px;
}
.el-tag i {
  margin-right: 6px;
}
.span {
  color: #303133;
}
</style>
